package com.novel.controller.other;

import com.novel.common.enums.UserIdentifyEnum;
import com.novel.common.pojo.RedisParamKey;
import com.novel.pojo.system.Recharge;
import com.novel.pojo.system.User;
import com.novel.utils.GsonTool;
import com.novel.utils.ParseRealIpUtils;
import com.novel.utils.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class BaseController {

    @Autowired
    protected HttpSession session;

    @Autowired
    protected HttpServletRequest request;

    @Autowired
    protected HttpServletResponse response;

    @Autowired
    protected RedisUtils redisUtils;

    /**
     * 获取充值信息
     */
    protected List<Recharge> getRechargeInfo() {
        String json = redisUtils.stringGet(RedisParamKey.rechargeList);
        return GsonTool.fromJsonList(json, Recharge.class);
    }

    /**
     * 获取请求用户的ip地址
     */
    protected String getUserIpAddress() {
        //从工具类获取用户ip地址
        return ParseRealIpUtils.parseRealIpAddress(request);
    }

    /**
     * 从session中获取用户信息
     */
    protected User getUserInformation() {
        String userInfo = redisUtils.hashGet(session.getId(), RedisParamKey.loginUser);
        if (StringUtils.isBlank(userInfo)) {
            log.info("没有获取到该用户的相关信息");
            return null;
        }
        return GsonTool.fromJson(userInfo, User.class);
    }

    /**
     * 设置用户登录信息到session中
     */
    protected void setUserInformation(User user) {
        //给thymeleaf做准备
        session.setAttribute("loginUser", user);
        redisUtils.hashSet(session.getId(), RedisParamKey.loginUser, GsonTool.toJson(user));
    }

    /**
     * 用户退出登录
     */
    protected void clearUserInformation() {
        //直接让session失效，删除他的各种登录信息
        redisUtils.deleteByKey(session.getId());
        session.invalidate();
    }

    /**
     * 检查用户权限
     */
    protected boolean checkUserPermission(UserIdentifyEnum identifyEnum, Integer permission) {
        List<Integer> permissionList;
        if (identifyEnum.equals(UserIdentifyEnum.READER)) {
            permissionList = Arrays.asList(1, 3, 5, 7);
        } else if (identifyEnum.equals(UserIdentifyEnum.AUTHOR)) {
            permissionList = Arrays.asList(2, 3, 7, 6);
        } else {
            permissionList = Arrays.asList(4, 5, 6, 7);
        }
        return permissionList.contains(permission);
    }

    /**
     * 设置验证码
     */
    protected void setVarCode(String code) {
        redisUtils.stringSet(session.getId() + "-" + RedisParamKey.varCode, code, 60);
    }

    /**
     * 获取验证码
     */
    protected String getVarCode() {
        return redisUtils.stringGet(session.getId() + "-" + RedisParamKey.varCode);
    }

    /**
     * 校验验证码
     * 验证通过，删除验证码缓存，验证失败继续保留
     */
    protected boolean compareVerCode(String receiveCode) {
        String varCode = getVarCode();

        if (StringUtils.isBlank(varCode)) {
            return false;
        }

        boolean result = StringUtils.equalsIgnoreCase(receiveCode, varCode);

        if (result) {
            //删除验证码缓存
            redisUtils.deleteByKey(session.getId() + "-" + RedisParamKey.varCode);
        }
        return result;

    }
}
